<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/12/1 0001
 * Time: 16:46
 */

namespace app\admin\logic;

use app\admin\model\Kindergarten as Kindergarten_model;
use think\Cache;

class Kindergarten
{
    private $kindergarten_model = '';

    public function __construct(){
        $this->kindergarten_model = new Kindergarten_model();
    }

    /**获取模型
     * @return Qudao_model|string
     */
    public function getModel()
    {
        if (empty($this->Kindergarten_model)) {
            $this->Kindergarten_model = new Kindergarten_model();
        }
        return $this->Kindergarten_model;
    }

    /**
     * 获取所有的幼儿园（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function kindergartensToText(){
        $states = self::getAllKindergartens();
        $states = collect($states)->column('name','id');
        return $states;
    }

    /**
     * 获得所有的幼儿园（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllKindergartens(){
        if (!empty(Cache::get('allKindergartens'))){
            return Cache::get('allKindergartens');
        }else{
            $states = Kindergarten_model::all();
            Cache::set('allKindergartens',$states);
            return $states;
        }
    }

    /**
     * 获取类别 select 数据
     * @return array
     * @author: Huang
     */
    public function getAllkindText()
    {
        $kinds = $this->getAllKindergartens();
        $kinders = collect($kinds)->where('status',1)->all();
        return $kinders;
    }

    /**
     * 获取启用的幼儿园
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getEnabledAreas(){
        $states = $this->getAllKindergartens();
        $states = collect($states)->where('status',1)->all();
        $states = collection($states)->toArray();
        $states = array_merge($states);
        return $states;
    }

    public function addKindergarten($data){
        if(empty($data)){
            return ['result'=>2,'msg'=>'数据丢失'];
        }
        $data['status'] = 1;//自动启用
        $result = $this->kindergarten_model->allowField(true)->validate(true)->save($data);
        if($result == false){
            return ['result'=>2,'msg'=>$this->kindergarten_model->getError()];
        }else{
            Cache::rm('allKindergartens');
            return ['result'=>1,'msg'=>'新增成功'];
        }
    }

    public function editKindergarten($data){
        if(empty($data)){
            return ['result'=>2,'msg'=>'数据丢失'];
        }
        if(empty($data['id'])){
            return ['result'=>2,'msg'=>'ID丢失'];
        }
        $result = $this->kindergarten_model->allowField(true)->validate('Kindergarten.edit')->isUpdate(true)->save($data);
        if($result == false){
            return ['result'=>2,'msg'=>$this->kindergarten_model->getError()?:'没有需要修改的数据'];
        }else{
            Cache::rm('allKindergartens');
            return ['result'=>1,'msg'=>'修改成功'];
        }
    }

    public function delKindergarten($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Kindergarten_model::get($id)) {
            if (Kindergarten_model::destroy($id)) {
                Cache::rm('allKindergartens');
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }



}